﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace Easydp.Models
{
    public class PushFeeManager
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public int Update(Entities.TT_PushFee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_PushFee set ");
            strSql.Append("NPAY=@NPAY,");
            strSql.Append("AgentGet=@AgentGet,");
            strSql.Append("AgentAccount=@AgentAccount,");
            strSql.Append("PlatformGet=@PlatformGet,");
            strSql.Append("PlatformAccount=@PlatformAccount,");
            strSql.Append("OtherGet=@OtherGet,");
            strSql.Append("OtherAccount=@OtherAccount,");
            strSql.Append("Dec=@Dec,");
            strSql.Append("CreateOn=@CreateOn");
            strSql.Append(" where OrderID=@OrderID ");
           
            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@OrderID", model.OrderID);
                updation.AddParameter("@NPAY", model.NPAY);
                updation.AddParameter("@AgentGet", model.AgentGet);
                updation.AddParameter("@AgentAccount", model.AgentAccount);
                updation.AddParameter("@PlatformGet", model.PlatformGet);
                updation.AddParameter("@PlatformAccount", model.PlatformAccount);
                updation.AddParameter("@OtherGet", model.OtherGet);
                updation.AddParameter("@OtherAccount", model.OtherAccount);
                updation.AddParameter("@Dec", model.Dec);
                updation.AddParameter("@CreateOn", model.CreateOn);

                return updation.ExecuteNonQuery();

            }
        }

        /// <summary>
        /// 得到model一个对象实体
        /// </summary>
        /// 


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entities.TT_PushFee GetModel(int OrderID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 OrderID,NPAY,AgentGet,AgentAccount,PlatformGet,PlatformAccount,OtherGet,OtherAccount,Dec,CreateOn from TT_PushFee ");
            strSql.Append(" where OrderID=@OrderID ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderID", SqlDbType.Int,4)};
            parameters[0].Value = OrderID;

            Entities.TT_PushFee model = new Easydp.Models.Entities.TT_PushFee();
            using (SqlText selection = new SqlText(strSql.ToString()))
            {
                selection.AddParameter("@OrderID", OrderID);
                while (selection.Read())
                {
                    if (selection["OrderID"].ToString() != "")
                    {
                        model.OrderID = int.Parse(selection["OrderID"].ToString());
                    }
                    model.NPAY = selection["NPAY"].ToString();
                    if (selection["AgentGet"].ToString() != "")
                    {
                        model.AgentGet = decimal.Parse(selection["AgentGet"].ToString());
                    }
                    model.AgentAccount = selection["AgentAccount"].ToString();
                    if (selection["PlatformGet"].ToString() != "")
                    {
                        model.PlatformGet = decimal.Parse(selection["PlatformGet"].ToString());
                    }
                    model.PlatformAccount = selection["PlatformAccount"].ToString();
                    if (selection["OtherGet"].ToString() != "")
                    {
                        model.OtherGet = decimal.Parse(selection["OtherGet"].ToString());
                    }
                    if (selection["OtherAccount"].ToString() != "")
                    {
                        model.OtherAccount = selection["OtherAccount"].ToString();
                    }
                    model.Dec = selection["Dec"].ToString();
                    if (selection["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selection["CreateOn"].ToString());
                    }

                }
            }
            return model;
        }




        /// <summary>
        /// 获得dataset数据列表
        /// </summary>
        public DataSet GetDataset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OrderID,NPAY,AgentGet,AgentAccount,PlatformGet,PlatformAccount,OtherGet,OtherAccount,Dec,CreateOn ");
            strSql.Append(" FROM TT_PushFee ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到list列表
        /// </summary>
        /// 
        public List<Entities.TT_PushFee> GetList(int OrderID)
        {
            List<Entities.TT_PushFee> list = new List<Easydp.Models.Entities.TT_PushFee>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OrderID,NPAY,AgentGet,AgentAccount,PlatformGet,PlatformAccount,OtherGet,OtherAccount,Dec,CreateOn ");
            strSql.Append(" FROM TT_PushFee ");
            strSql.Append(" where OrderID=@OrderID ");
            using (SqlText selectlist = new SqlText(strSql.ToString()))
            {
                selectlist.AddParameter("@OrderID", OrderID);
                while (selectlist.Read())
                {
                    Entities.TT_PushFee model = new Easydp.Models.Entities.TT_PushFee();
                    if (selectlist["OrderID"].ToString() != "")
                    {
                        model.OrderID = int.Parse(selectlist["OrderID"].ToString());
                    }
                    model.NPAY = selectlist["NPAY"].ToString();
                    if (selectlist["AgentGet"].ToString() != "")
                    {
                        model.AgentGet = decimal.Parse(selectlist["AgentGet"].ToString());
                    }
                    model.AgentAccount = selectlist["AgentAccount"].ToString();
                    if (selectlist["PlatformGet"].ToString() != "")
                    {
                        model.PlatformGet = decimal.Parse(selectlist["PlatformGet"].ToString());
                    }
                    model.PlatformAccount = selectlist["PlatformAccount"].ToString();
                    if (selectlist["OtherGet"].ToString() != "")
                    {
                        model.OtherGet = decimal.Parse(selectlist["OtherGet"].ToString());
                    }
                    model.OtherAccount = selectlist["OtherAccount"].ToString();
                    model.Dec = selectlist["Dec"].ToString();
                    if (selectlist["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selectlist["CreateOn"].ToString());
                    }
                    list.Add(model);
                }
            }
            return list;
        }

        public  void Insert(Entities.TT_Orders obj)
        {
            Entities.TT_AccountInfo accountInfoObj = (new AccountInfoManager()).GetAccountByAccountType("alipay",obj.AgentID.ToString());
            if (accountInfoObj != null)
            {
                decimal agentGet = obj.PayMoney - obj.PayPoundage;// 
                decimal jsAgentGet = (obj.Amount * obj.UnitPrice) + obj.TotalMoney + 5 * obj.Amount - 1 * obj.Amount;
                decimal platGet = 0;//obj.PayMoney - agentGet;

                decimal platGetint = obj.PayPoundage;
                string dec = string.Format("实际支付：{0},代理商获取:{1};平台获取：{2};支付手续费:{3};",obj.PayMoney,agentGet,platGet,obj.PayPoundage);
                using (SqlStoredProcedure insertion = new SqlStoredProcedure("Pro_TT_PushFee_Pan_Insert"))
                {
                    insertion.AddParameter("@OrderID", obj.OrderID);
                    insertion.AddParameter("@NPAY", "alipay");
                    insertion.AddParameter("@AgentGet", agentGet);
                    insertion.AddParameter("@AgentAccount", accountInfoObj.AccountCode);
                    insertion.AddParameter("@PlatformGet",platGet);
                    insertion.AddParameter("@PlatformAccount", "easydp@yeah.net");
                    insertion.AddParameter("@OtherGet", obj.PayPoundage);
                    insertion.AddParameter("@OtherAccount", "panbbb@hotmail.com");
                    insertion.AddParameter("@Dec",DBNull.Value);
                    insertion.ExecuteNonQuery();
                  
                }


            }
             
        }
        
    }
}
